package com.mvd.app.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.util.TextInfo;
import com.kongzue.dialog.v3.BottomMenu;
import com.kongzue.dialog.v3.MessageDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.activity.BaseActivity;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.JsonUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.CarsInforModel;
import com.mvd.app.model.CheckItemModel;
import com.mvd.app.model.WaiJianItemModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.iwgang.countuptime.CountupView;

import static com.mvd.app.application.BaseApplication.checkItemModelList;

/**
 * 车辆详情 页面
 */

public class CarActivity extends BaseActivity {


    @BindView(R.id.ac_car_number)
    TextView acCarNumber;
    @BindView(R.id.ac_car_SN)
    TextView acCarSN;
    @BindView(R.id.ac_car_VIN)
    TextView acCarVIN;
    @BindView(R.id.ac_car_times)
    TextView acCarTimes;
    @BindView(R.id.ac_car_line)
    TextView acCarLine;
    @BindView(R.id.ac_car_lianwang)
    Button acCarLianwang;
    @BindView(R.id.ac_car_weiyixing)
    Button acCarWeiyixing;
    @BindView(R.id.ac_car_waijian)
    Button acCarWaijian;
    @BindView(R.id.ac_car_luntai_guige)
    TextView acCarLuntaiGuige;
    private int CHECK_MODE = 0;//检测模式 0:外检  1：路试 2：底盘动态
    private String Line = "01";//外检车道
    private String Item_Code = "";//F1外检,R1路试，DC底盘动态
    private String Login_name = "", login_IDNumber;//当前登录的检测员的名字
    private CarsInforModel carsInforModel;
    public CarActivity instances = null;
    public boolean NQ_STATEMENT = false;//判定按钮是否可点击  ---联网查询
    public boolean UC_STATEMENT = false;//判定按钮是否可点击  ---唯一性
    private CountupView mCvCountupViewTest1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_car);
        ButterKnife.bind(this);
        setBackBtn();
        setTopTitle("车辆详情");
        String logStrf = "\n" + "onCreate-----车辆详情-----CarActivity---" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
        instances = this;
        mCvCountupViewTest1 = (CountupView) findViewById(R.id.ac_car_tv_jishiqi_cv_CountupViewTest3);
        mCvCountupViewTest1.setTag("test1");
        long time1 = (long) 1000;
        mCvCountupViewTest1.start(time1);
        carsInforModel = (CarsInforModel) getIntent().getSerializableExtra("model_CarsInforModel");
        login_IDNumber = getIntent().getExtras().getString("login_IDNumber", "");
        Login_name = getIntent().getExtras().getString("Login_name", "");
        CHECK_MODE = BaseApplication.JIANCE_MODE;
        switch (CHECK_MODE) {
            case 0:
                Item_Code = "F1";
                break;
            case 1:
                Item_Code = "R1";
                break;
            case 2:
                Item_Code = "DC";
                break;
        }
        if (carsInforModel != null) {

            //轮胎规格
            if (!TextUtils.isEmpty(carsInforModel.getTyreSize())) {
                acCarLuntaiGuige.setText(carsInforModel.getTyreSize());
            }
            //车牌号码
            if (!TextUtils.isEmpty(carsInforModel.getPlateRegion()) &&
                    !TextUtils.isEmpty(carsInforModel.getPlateNO())) {
                acCarNumber.setText(carsInforModel.getPlateRegion() +
                        carsInforModel.getPlateNO());
            }
            //检验次数```````
            if (!TextUtils.isEmpty(String.valueOf(carsInforModel.getTimes()))) {
                acCarTimes.setText(carsInforModel.getTimes() + "次");
            }
            //SN
            if (!TextUtils.isEmpty(String.valueOf(carsInforModel.getSN()))) {
                acCarSN.setText(carsInforModel.getSN());
            }
            //VIN
            if (!TextUtils.isEmpty(String.valueOf(carsInforModel.getVIN()))) {
                acCarVIN.setText(carsInforModel.getVIN());
            }
        }
        List<WaiJianItemModel> books = WaiJianItemModel.listAll(WaiJianItemModel.class);
        if (books != null && books.size() > 0) {
            UtilsLog.e("CarActivity----本地数据库查询----不为空==" + books.toString());
            for (int i = 0; i < books.size(); i++) {
                WaiJianItemModel.delete(books.get(i));
            }
        } else {
            UtilsLog.e("CarActivity----本地数据库查询----空==");
        }

        getGET_ALL_ITEMList();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        instances = null;
        mCvCountupViewTest1.stop();
        String logStrf = "\n" + "onDestroy-----车辆详情-----CarActivity---" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
        List<WaiJianItemModel> books = WaiJianItemModel.listAll(WaiJianItemModel.class);
        if (books != null && books.size() > 0) {
            for (int i = 0; i < books.size(); i++) {
                WaiJianItemModel.delete(books.get(i));
            }
        }
    }

    @OnClick({R.id.ac_car_line, R.id.ac_car_lianwang,
            R.id.ac_car_weiyixing, R.id.ac_car_waijian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_car_line:
                //检测线号
                showMyDialog(line_str, R.id.ac_car_line);
                break;
            case R.id.ac_car_lianwang:
                Line = acCarLine.getText().toString().trim();
                if (carsInforModel.getNQStatus().equals("0") && NQ_STATEMENT == false) {
                    Intent i_1 = new Intent(this, OnlineActivity.class);
                    i_1.putExtra("model_CarsInforModel", carsInforModel);
                    i_1.putExtra("Car_Line", Line);
                    i_1.putExtra("login_w_name", Login_name);
                    i_1.putExtra("login_IDNumber", login_IDNumber);
                    startActivityForResult(i_1, 101);
//                    startActivity(i_1);
                } else {
                    MessageDialog.show(instances, "提示", "项目已经检测完成", "确定");
                }
                break;
            case R.id.ac_car_weiyixing:
                Line = acCarLine.getText().toString().trim();
                if (carsInforModel.getUCStatus().equals("0") && UC_STATEMENT == false) {
                    Intent i_2 = new Intent(this, UniquenessActivity.class);
                    i_2.putExtra("model_CarsInforModel", carsInforModel);
                    i_2.putExtra("Car_Line", Line);
                    i_2.putExtra("login_w_name", Login_name);
                    i_2.putExtra("login_IDNumber", login_IDNumber);
                    startActivity(i_2);
                } else {
                    MessageDialog.show(instances, "提示", "项目已经检测完成", "确定");
                }
                break;
            case R.id.ac_car_waijian:
                Line = acCarLine.getText().toString().trim();
                Intent i_3 = new Intent(this, WaiJianActivity.class);
                i_3.putExtra("model_CarsInforModel", carsInforModel);
                i_3.putExtra("Car_Line", Line);
                i_3.putExtra("login_w_name", Login_name);
                i_3.putExtra("login_IDNumber", login_IDNumber);
                startActivity(i_3);
//                if (!TextUtils.isEmpty(Item_Code)) {
//                    if (CHECK_MODE == 0) {
////                        getDataWaiJainStart("");
////                        String logStr = "车牌号码：：" + carsInforModel.getPlateRegion() + carsInforModel
////                                .getPlateNO() + "\n" + "动作：点击项目开始按钮";
////                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                    } else {
//
//                    }
//                }
                break;
        }

    }

    private String[] line_str = new String[]{"01", "02", "03", "04", "05", "06", "07", "08"};
    private String[] fou_shi_str = new String[]{"否", "是"};

    private void showMyDialog(String[] str, final int id) {
//        BottomMenu bottomMenu= BottomMenu().build(this);
        BottomMenu.show(this, str, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
                UtilsLog.e("外检---showMyDialog==" + text + "===" + index);
                switch (id) {
                    case R.id.ac_car_line:
                        // 线号
                        acCarLine.setText(text);
                        break;
                }
            }
        }).setMenuTextInfo(new TextInfo().setBold(true).setFontColor(Color.BLACK).setGravity(Gravity.CENTER));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            UtilsLog.e("onActivityResult=resultCode=" + resultCode);
            UtilsLog.e("onActivityResult==" + data.getIntExtra("nq", 0));
            switch (resultCode) {
                case 101:
                    int result_1 = data.getIntExtra("nq", 0);
                    if (result_1 == 1) {
                        NQ_STATEMENT = true;
                    } else {
                        NQ_STATEMENT = false;
                    }
                    break;
                case 102:
                    int result_2 = data.getIntExtra("nq", 0);
                    if (result_2 == 1) {
                        UC_STATEMENT = true;
                    } else {
                        UC_STATEMENT = false;
                    }
                    break;
            }
        }
    }


    //获取所有人工检验项目接口
    private void getGET_ALL_ITEMList() {
        String url = "";
//        if (SharedPreferencesUtils.get38900(instances)) {
        url = GET_ALL_ITEM38900;
//        } else {
//            url = GET_ALL_ITEM;
//        }
        String logStr = "\n" + "获取所有人工检验项目接口----" + "\n" + "URL::" +
                SharedPreferencesUtils.getIP(instances) +
                url + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("getGET_ALL_ITEMList---url==" + SharedPreferencesUtils.getIP(instances) + url);
        OkGo.<String>get(SharedPreferencesUtils.getIP(instances) + url).tag(instances)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
//                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
//                        CommonUtils.hideLoadingDialog(instances);
                        String result = response.body();
//                        UtilsLog.e("getGET_ALL_ITEMList-result==" + result);
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        UtilsLog.e("getGET_ALL_ITEMList-newResult==" + newResult);
//                        String logStr = "\n" + "获取所有人工检验项目接口----onSuccess" + "\n" + "result::" + response.body() + "\n";
//                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        if (!TextUtils.isEmpty(newResult) && !"[[]]".equals(newResult) && !"[{}]".equals
                                (newResult) && !"[]".equals(result)) {
                            checkItemModelList = new ArrayList<CheckItemModel>();
                            checkItemModelList = JsonUtil.stringToArray(newResult, CheckItemModel[].class);
//                            for (int i = 0; i < checkItemModelList.size(); i++) {
//                                UtilsLog.e("checkItemModelList=11=" + checkItemModelList.get(i).toString());
//                            }

                        } else {
//                            Toast.makeText(instances, getResources().getString(R.string.network_error),
// Toast.LENGTH_LONG).show();
                            UtilsLog.e("getGET_ALL_ITEMList-result==" + "没有数据");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
//                        CommonUtils.hideLoadingDialog(instances);
                        UtilsLog.e("getGET_ALL_ITEMList-onError==" + response.body());
                        String logStr = "\n" + "获取所有人工检验项目接口----onError" + "\n" + "result::" + response.body() + "\n";
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        Toast.makeText(CarActivity.this, getResources().getString(R.string.services_error), Toast
                                .LENGTH_LONG).show();
                    }
                });
    }


}
